package com.xca.znb.view;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import com.xca.znb.R;
import com.xca.znb.adapter.ProcurementManagerAdapter;
import com.xca.znb.adapter.SchoolFragmentAdapter;
import com.xca.znb.entity.News;
import com.xca.znb.model.impl.SchoolFragmentImpl;
import com.xca.znb.presenter.SchoolFragmentPresenter;

import java.util.ArrayList;
import java.util.List;

public class SaleSchoolFragment extends Fragment implements View.OnClickListener{
    private View sale_school_fragment ;
    private ImageButton Publish ;
    private TextView Tab1 , Tab2 , Tab3 , Tab4 , Tab5 , Tab6 , Tab7 , Tab8 , Tab9 ;
    private ListView showList ;

    private SchoolFragmentPresenter presenter ;
    private List<News> newsList ;
    private SchoolFragmentAdapter adapter ;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (sale_school_fragment == null){
            sale_school_fragment = inflater.inflate(R.layout.school_fragment , container , false);
        }
        Publish = sale_school_fragment.findViewById(R.id.school_publish_btn) ;
        Tab1 = sale_school_fragment.findViewById(R.id.school_title1) ;
        Tab2 = sale_school_fragment.findViewById(R.id.school_title2) ;
        Tab3 = sale_school_fragment.findViewById(R.id.school_title3) ;
        Tab4 = sale_school_fragment.findViewById(R.id.school_title4) ;
        Tab5 = sale_school_fragment.findViewById(R.id.school_title5) ;
        Tab6 = sale_school_fragment.findViewById(R.id.school_title6) ;
        Tab7 = sale_school_fragment.findViewById(R.id.school_title7) ;
        Tab8 = sale_school_fragment.findViewById(R.id.school_title8) ;
        Tab9 = sale_school_fragment.findViewById(R.id.school_title9) ;
        showList = sale_school_fragment.findViewById(R.id.school_news_list) ;


        SchoolFragmentImpl schoolFragment = new SchoolFragmentImpl(getActivity());
        presenter = new SchoolFragmentPresenter(schoolFragment);


        Tab1.setOnClickListener(this);
        Tab2.setOnClickListener(this);
        Tab3.setOnClickListener(this);
        Tab4.setOnClickListener(this);
        Tab5.setOnClickListener(this);
        Tab6.setOnClickListener(this);
        Tab7.setOnClickListener(this);
        Tab8.setOnClickListener(this);
        Tab9.setOnClickListener(this);
        Publish.setOnClickListener(this);

        Tab1.setSelected(true);


        newsList = presenter.getNewsByType("热点新闻");
        adapter = new SchoolFragmentAdapter(getActivity() , newsList);
        showList.setAdapter(adapter);
        adapter.notifyDataSetChanged();


        showList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView showId = view.findViewById(R.id.school_news_list_id);
                String sId = showId.getText().toString();
                Intent intent = new Intent(getActivity() , SchoolShowNews.class);
                intent.putExtra("schoolFragment_showNews" , sId);
                startActivity(intent);
            }
        });

        return sale_school_fragment ;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.school_title1){
            Tab1.setSelected(true);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab1.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId() ==R.id.school_title2) {
            Tab1.setSelected(false);
            Tab2.setSelected(true);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab2.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title3) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(true);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab3.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title4) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(true);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab4.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title5) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(true);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab5.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title6) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(true);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab6.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title7) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(true);
            Tab8.setSelected(false);
            Tab9.setSelected(false);

            String type = Tab7.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title8) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(true);
            Tab9.setSelected(false);

            String type = Tab8.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (v.getId() ==R.id.school_title9) {
            Tab1.setSelected(false);
            Tab2.setSelected(false);
            Tab3.setSelected(false);
            Tab4.setSelected(false);
            Tab5.setSelected(false);
            Tab6.setSelected(false);
            Tab7.setSelected(false);
            Tab8.setSelected(false);
            Tab9.setSelected(true);

            String type = Tab9.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        } else if (v.getId() == R.id.school_publish_btn) {
            showSelectionDialog();
        }
    }
    private void showSelectionDialog() {
        List<String> options = new ArrayList<>();
        options.add("热点新闻");
        options.add("专家咨询");
        options.add("支农宝新闻");
        options.add("创业项目");
        options.add("动植物疫情预报");
        options.add("植物病虫害");
        options.add("动物病害");
        options.add("农业百科");
        options.add("气象预报");

        final String[] itemsArray = options.toArray(new String[0]);

        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle("选择发布的文章类型");

        builder.setSingleChoiceItems(itemsArray, 0, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // 这里可以根据需要处理点击事件，但通常不需要，因为确认按钮处理最终的选择
            }
        });

        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
                int selectedPosition = ((AlertDialog)dialog).getListView().getCheckedItemPosition();
                if (selectedPosition != -1) {
                    String selectedOption = itemsArray[selectedPosition];
                } else {
                    showToast("请选择发布类型");
                }
                Intent intent = new Intent(getActivity() , SchoolEssayActivity.class);
                intent.putExtra("publish_type" , selectedPosition);
                startActivity(intent);
            }
        });

        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
            }
        });

        AlertDialog alertDialog = builder.create();
        alertDialog.show();
    }

    private void showToast(String message) {
        Toast.makeText(getActivity(), message, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResume() {
        super.onResume();
        // 刷新数据
        SharedPreferences sharedPreferences = getActivity().getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
        String login_phone = sharedPreferences.getString("phone" , "");
        if (Tab1.isSelected()){
            String type = Tab1.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab2.isSelected()){
            String type = Tab2.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab3.isSelected()){
            String type = Tab3.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab4.isSelected()){
            String type = Tab4.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab5.isSelected()){
            String type = Tab5.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab6.isSelected()){
            String type = Tab6.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab7.isSelected()){
            String type = Tab7.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab8.isSelected()){
            String type = Tab8.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }else if (Tab9.isSelected()){
            String type = Tab9.getText().toString() ;
            newsList = presenter.getNewsByType(type);
            adapter = new SchoolFragmentAdapter(getActivity() , newsList);
            showList.setAdapter(adapter);
            adapter.notifyDataSetChanged();
        }
    }

}
